package com.dreamer.view.pyramid;

import com.dreamer.domain.lottery.Activity;
import com.dreamer.domain.lottery.Prize;
import com.dreamer.domain.pyramid.Pyramid;
import com.dreamer.domain.pyramid.PyramidUser;
import com.dreamer.domain.pyramid.PyramidUserVo;
import com.dreamer.domain.user.User;
import com.dreamer.service.lottery.ActivityHandler;
import com.dreamer.service.pyramid.PyramidHandler;
import com.dreamer.service.pyramid.PyramidUserHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ps.mx.otter.utils.SearchParameter;
import ps.mx.otter.utils.WebUtil;
import ps.mx.otter.utils.message.Message;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/pyramidUser")
public class PyramidUserQueryController {

    @Autowired
    private PyramidUserHandler pyramidUserHandler;

    @Autowired
    private ActivityHandler activityHandler;

    @RequestMapping("/index.html")
    public String index(
            @ModelAttribute("parameter") SearchParameter<PyramidUser> parameter,
            HttpServletRequest request, Model model) {
        try {
            List<PyramidUser> list = pyramidUserHandler.searchAllByPage(parameter);
            WebUtil.turnPage(parameter, request);
            model.addAttribute("PyramidVoList", list);
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        return "pyramid/pyramidUser_index";
    }

//    @RequestMapping("/index.html")
//    public String index(Model model,@ModelAttribute("parameter") SearchParameter<PyramidUserVo> parameter) {
//        try {
//            List<PyramidUserVo> list = pyramidUserHandler.findPyramidVoAll(parameter);
//            model.addAttribute("PyramidVoList", list);
//        } catch (Exception exp) {
//            exp.printStackTrace();
//        }
//        return "pyramid/pyramidUser_index";
//    }

    @RequestMapping(value = "/edit.html", method = RequestMethod.GET)
    public String edit_enter(@ModelAttribute("parameter") SearchParameter<Prize> parameter, HttpServletRequest request,Model model) {
        User user = (User) WebUtil.getCurrentUser(request);
        if (user.isAdmin()) {
            List<Activity> activityList = activityHandler.findAll();
            model.addAttribute("activityList", activityList);
            return "lottery/prize_edit";
        } else {
            return "common/403";
        }
    }

    @ModelAttribute("parameter")
    public SearchParameter<PyramidUser> preprocess(@RequestParam("id") Optional<Integer> id) {
        SearchParameter<PyramidUser> parameter = new SearchParameter<>();
        PyramidUser  pyramidUser;
        if (id.isPresent()) {
            pyramidUser = pyramidUserHandler.findById(id.get());
        }else {
            pyramidUser=new PyramidUser();
        }
        parameter.setEntity(pyramidUser);
        return parameter;
    }

//    @ModelAttribute("parameter")
//    public SearchParameter<PyramidUserVo> preprocess(@RequestParam("id") Optional<Integer> id) {
//        //id=Optional.of(1);
//        SearchParameter<PyramidUserVo> parameter = new SearchParameter<>();
//        PyramidUserVo  pyramidUserVo;
//        if (id.isPresent()) {
//            pyramidUserVo = pyramidUserHandler.findByIdVo(id.get());
//        }else {
//            pyramidUserVo=new PyramidUserVo();
//        }
//        parameter.setEntity(pyramidUserVo);
//        return parameter;
//    }

    @ResponseBody
    @RequestMapping(value = "/encrypt.json", method = RequestMethod.POST)
    public Message encrypt(String encryptLotteryCode) {
        byte[] b = null;
        String s = null;
        try {
            b = encryptLotteryCode.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
            s = new BASE64Encoder().encode(b);
        }
        return Message.createSuccessMessage("success",s);
    }

}
